package com.example.memo.Adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.memo.HomeFragment;
import com.example.memo.R;
import com.example.memo.db.NoteHelper;
import com.example.memo.entity.NoteInfo;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.NoteHolder> {
    private List<NoteInfo> noteList;
    private onItemClickListener onItemClickListener;
    private NoteInfo noteInfo;

    public NoteAdapter() {
    }

    public void addNote(List<NoteInfo> noteList){
        sort(noteList);
        this.noteList = noteList;
        notifyDataSetChanged();
    }

    private void sort(List<NoteInfo> noteList){
        noteList.sort((note1, note2) -> {
            return note2.getIstop() - note1.getIstop();
        });
    }

    @NonNull
    @Override
    public NoteHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.note_item,parent,false);
        NoteHolder noteHolder = new NoteHolder(view);
        return noteHolder;
    }

    @Override
    public int getItemCount() {
        if(noteList == null){
            return 0;
        }else{
            return noteList.size();
        }
    }

    @Override
    public void onBindViewHolder(@NonNull NoteHolder holder, @SuppressLint("RecyclerView") int position) {
        noteInfo = noteList.get(position);

        holder.date.setText(noteInfo.getDate());
        holder.title.setText(noteInfo.getTitle());
        holder.content.setText(noteInfo.getContent());
        if(noteInfo.getIstop() == 1){
            holder.top_up.setVisibility(View.VISIBLE);
        }else {
            holder.top_up.setVisibility(View.GONE);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(onItemClickListener != null){
                    onItemClickListener.onItemClick(position);
                }
            }
        });
        holder.more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(onItemClickListener != null){
                    onItemClickListener.onMore(position);
                }
            }
        });
    }

    public void setOnItemClickListener(NoteAdapter.onItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    class NoteHolder extends RecyclerView.ViewHolder{
        TextView date,title,content;
        ImageView top_up,more;

        public NoteHolder(@NonNull View itemView) {
            super(itemView);
            date = itemView.findViewById(R.id.date);
            title = itemView.findViewById(R.id.title);
            content = itemView.findViewById(R.id.content);
            top_up = itemView.findViewById(R.id.top_up);
            more = itemView.findViewById(R.id.more);
        }
    }

    public interface onItemClickListener{
        void onItemClick(int position);
        void onMore(int position);
    }
}
